/*
    file read/write (Firefox, IE)
    Copyright (C) 2008 Yuriy Butenko y.v.butenko@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/* Open flags 
	#define PR_RDONLY       0x01
	#define PR_WRONLY       0x02
	#define PR_RDWR         0x04
	#define PR_CREATE_FILE  0x08
	#define PR_APPEND      0x10
	#define PR_TRUNCATE     0x20
	#define PR_SYNC         0x40
	#define PR_EXCL         0x80
*/
/*
	** File modes ....
	**
	** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
	** The 'mode' argument may be ignored by PR_Open on other platforms.
	**
	**   00400   Read by owner.
	**   00200   Write by owner.
	**   00100   Execute (search if a directory) by owner.
	**   00040   Read by group.
	**   00020   Write by group.
	**   00010   Execute by group.
	**   00004   Read by others.
	**   00002   Write by others
	**   00001   Execute by others.
*/
fio = {}
fio.readFF = function ( fn ) {
	try {
		netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );
	} catch ( e ) {
		return null; /* Permission to read file was denied. */
	};
	var file = Components.classes[ "@mozilla.org/file/local;1" ]
			.createInstance( Components.interfaces.nsILocalFile );
	try { file.initWithPath( fn ); } catch ( e ) { return null; };
	if ( file.exists() == false ) {
		return null; /* Not exists */
	};
	var is = Components.classes[ "@mozilla.org/network/file-input-stream;1" ]
			.createInstance( Components.interfaces.nsIFileInputStream );
	is.init( file, 0x01, 00004, null );
	var sis = Components.classes[ "@mozilla.org/scriptableinputstream;1" ]
			.createInstance( Components.interfaces.nsIScriptableInputStream );
	sis.init( is );
	return sis.read( sis.available() );
}
fio.writeFF = function ( fn, txt ) {
	try {
		netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );
	} catch ( e ) {
		return null; /* Permission to save file was denied. */
	};
	var file = Components.classes[ "@mozilla.org/file/local;1" ]
			.createInstance( Components.interfaces.nsILocalFile );
	try { file.initWithPath( fn ); } catch ( e ) { return null; };
	if ( file.exists() == false ) {
		file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
	};
	var outputStream = Components.classes[ "@mozilla.org/network/file-output-stream;1" ]
			.createInstance( Components.interfaces.nsIFileOutputStream );
	outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
	var result = outputStream.write( txt, txt.length );
	outputStream.close();
	return result;
}
fio.readIE = function ( fn ) {
	var txt;
	try {
		var fso = new ActiveXObject( "Scripting.FileSystemObject" );
		var f = fso.OpenTextFile( fn, 1 );
		txt = f.ReadAll();
		f.close();
	} catch (e) {
		return null;
	};
	return txt;
}
fio.writeIE = function ( fn, txt ) {
	try {
		var fso = new ActiveXObject( "Scripting.FileSystemObject" );
		var f = fso.CreateTextFile( fn, true );
	f.Write( txt );
		f.close();
	} catch (e) {
		return null;
	};
	return txt.length;
}
fio.read = function ( fn ) {
	var uA = navigator.userAgent;
	if ( uA.indexOf( "Opera" ) > -1 ) {
	} else if ( uA.indexOf( "Gecko" ) > -1 ) {
		return fio.readFF( fn );
	} else if ( uA.indexOf( "MSIE" ) > -1 ) {
		return fio.readIE( fn );
	};
	alert( "Sorry! This Browser do not read to locale file system." );
	return null;
}
fio.write = function ( fn, txt ) {
	var uA = navigator.userAgent;
	if ( uA.indexOf( "Opera" ) > -1 ) {
	} else if ( uA.indexOf( "Gecko" ) > -1 ) {
		return fio.writeFF( fn, txt );
	} else if ( uA.indexOf( "MSIE" ) > -1 ) {
		return fio.writeIE( fn, txt );
	};
//	alert( "Sorry! This Browser do not write to locale file system." );
	return null;
}
fio.testIO = function () {
//	return false;
	var uA = navigator.userAgent;
	if ( uA.indexOf( "Opera" ) > -1 ) {
	} else if ( uA.indexOf( "Gecko" ) > -1 ) {
		return true;
	} else if ( uA.indexOf( "MSIE" ) > -1 ) {
		return true;
	};
	return false;
}
